//
//  UserAccountViewModel.swift
//  LRWeibo
//
//  Created by admin on 15/9/25.
//  Copyright © 2015年 懒人. All rights reserved.
// 计算型属性和懒加载属性区别 = （）

import Foundation

class UserAccountViewModel{

    var userAccount: LRUserAccount?
    
    /// 头像路径
    var avatar_large: String?{
        return userAccount?.avatar_large ?? ""
    }
    
    /// touken
    var access_token: String?{
        return userAccount?.access_token
    }
    
    /// 是否登录
    var isLogin: Bool{
    
        return userAccount != nil
    }
    
    /// 保存路径
    var savePath: String?{
        let p = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0]
        return (p as NSString).stringByAppendingPathComponent("useraccount.plist")
    }
    
     // MARK: - 单利设置
    static let sharedUserAccount = UserAccountViewModel()
    
     // MARK: - 初始化
    init()
    {
        userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(savePath!) as? LRUserAccount
        
        // 判断是否过期
        if let date = userAccount?.expires_date
        {
            if date.compare(NSDate()) != NSComparisonResult.OrderedDescending{
                
                // 过期
                userAccount = nil
            }

        }
    }
    
    
}